// ScriptParser.h: interface for the ScriptParser class.
//
//////////////////////////////////////////////////////////////////////

#ifndef SCRIPTPARSER_H
#define SCRIPTPARSER_H

// define the following to get additional debug dump to the console

//#define SCRIPT_PARSER_DEBUG_SHOW_TOEKN
//#define SCRIPT_PARSER_DEBUG_DUMP_FILEDATA
//#define SCRIPT_PARSER_DEBUG_DUMP_GENERAL

#define DUPLICATE_TOKEN(destination_, source_) \
{ \
	destination_ = new char[strlen(source_) + 1]; \
	memcpy(destination_, source_, strlen(source_) + 1); \
} \

class ScriptParser  
{
public:

	char*	token;		// points to the token that was parsed. 


	char *filename;		// name of the script file						
	char *fileData;		// the bytes of the file
	int  fileSize;		


	// delim strings that are checked for when parsing next token
	char* delimString;
	char* delimWhiteSpace;
	char* delimSeparator;

	int		delimSeparatorLength,
			delimWhiteSpaceLength,
			delimStringLength;


	
	int		fileDataNextIndex,			//if any of these is == fileSize , they have passed the end of the file. (we have reached the end of the file)
			fileDataCurrentIndex;

	char	lastDelimParsed;				// the delim that terminated last token
	int		lastDelimParsedIndex;			// and its corresponding index	


	ScriptParser();
	virtual ~ScriptParser();

	// creates and initializes the parser using the provided file name
	bool  Create(char *filename);
	
	// gets a string deliminated by delimSeparator
	char* NextToken();

	// gets a string deliminated by user supplied delim
	char* NextToken(char *delim);

	// assumes the next token is a quoted string and returns the string ignoring separators in the quotation 
	char* NextTokenQuotedString();
	
	// compares the begining of the token with the passed in string
	// return wheater there was a match
	bool TokenBeginsWith(char *string);

	// puts token back so the next read token starts at the begining of current token again
	void TokenPush();

	// helper function that make a new string and copies token into it. 
	// assumes token is not NULL
	// NOTE: caller is responsible for freeing the returned copy
	char* TokenCreateCopy();	
	
	// return whether or not it skipped anything
	bool SkipCharsInFront(char *chars);

	// returns the current token as a long
	long  GetTokenAsLong();

	//
	void RemoveComments(char* commentDelim);

};

#endif // SCRIPTPARSER_H